<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\UploadFile;
use App\Http\Requests\InsertCourseRequest;
use App\Models\Category;
use App\Models\Course;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

class CourseController extends Controller
{
    use UploadFile;

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $data = Course::with('category')->paginate();
        return view('admin.course.index', compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        // 分类
        $data = Category::all(['id', 'name']);
        return view('admin.course.create', compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param InsertCourseRequest $request
     * @return array
     */
    public function store(InsertCourseRequest $request)
    {
        $data = $request->all();
        $data['cover'] = '';
        if ($request->hasFile('file')) {
//            上传成功，调用上传方法
            $fileinfo = $this->up($request->file('file'));
            $fileinfo && $data['cover'] = '/uploads/' . $fileinfo->getFilename();
        }

        $res = Course::create($data);
        if ($res) return ['status' => 0, 'msg' => '添加成功', 'url' => $data['cover']];
        return ['status' => 1, 'msg' => '添加失败'];
    }


    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return Response
     */
    public function edit($id)
    {
        $course = Course::with('category')->find($id);
        // 分类
        $data = Category::all(['id', 'name']);
        return view('admin.course.edit', compact('data', 'course'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param InsertCourseRequest $request
     * @param  int $id
     * @return array
     */
    public function update(InsertCourseRequest $request, $id)
    {
        $data = $request->only(['name','teacher','category_id','intro','cover']);
        if ($request->hasFile('file')) {
//            上传成功，调用上传方法
            $fileinfo = $this->up($request->file('file'));
            $fileinfo && $data['cover'] = '/uploads/' . $fileinfo->getFilename();
        }
        $res = Course::where('id', $id)->update($data);
        if ($res) return ['status' => 0, 'msg' => '修改成功'];
        return ['status' => 1, 'msg' => '修改失败'];
    }

    /**
     * 删除
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return JsonResponse
     */
    public function destroy($id)
    {
        $res = Course::destroy($id);
        if ($res) return response()->json(['status' => 0, 'msg' => '成功']);
        return response()->json(['status' => 1, 'msg' => '失败']);
    }
}
